package com.quan.common.core;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quan.common.bean.PageBean;

/**
 * 公共基础业务接口实现类
 * @author 杨浩泉
 *
 * @param <M>
 * @param <T>
 */
public class MyBaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements MyBaseService<T> {

	@Override
	public IPage<T> selectPage(T entity, PageBean pageBean) {
		Page<T> page = new Page<T>(pageBean.getPage(), pageBean.getLimit());

		return super.page(page, new QueryWrapper<T>(entity));
	}

	@Override
	public IPage<T> selectPage(T entity, PageBean pageBean, boolean isSearchCount) {
		Page<T> page = new Page<>(pageBean.getPage(), pageBean.getLimit(), isSearchCount) ;

		return super.page(page, new QueryWrapper<T>(entity));
	}

	@Override
	public IPage<T> selectPage(QueryWrapper<T> queryWrapper, PageBean pageBean) {
		Page<T> page = new Page<>(pageBean.getPage(), pageBean.getLimit()) ;
		return super.page(page, queryWrapper);
	}
	
	
	@Override
	public boolean hasExist(String column, Object val, Class<T> entity) {
		return this.hasExist(column, val, entity, null) ;
	}
	
	@Override
	public boolean hasExist(String column, Object val, Class<T> entity, Map<String, Object> queryMaps) {
		QueryWrapper<T> queryWrapper = new QueryWrapper<T>() ;
		queryWrapper.eq(column, val);
		
		if(null != queryMaps) {
			for (String key : queryMaps.keySet()) {
				queryWrapper.eq(key, queryMaps.get(key)) ;
			}
		}
		
		int count = super.count(queryWrapper);
		if(count == 0) {
			return false;	// 不存在
		} else {
			return true;	// 存在
		}
	}
	
	@Override
	public T hasExist(String column, Object val, Map<String, Object> queryMaps, String...selectColumns) {
		QueryWrapper<T> queryWrapper = new QueryWrapper<T>() ;
		queryWrapper.eq(column, val);
		if(null != selectColumns && selectColumns.length > 0) {
			queryWrapper.select(selectColumns);
		}
		
		if(null != queryMaps) {
			for (String key : queryMaps.keySet()) {
				queryWrapper.eq(key, queryMaps.get(key)) ;
			}
		}
		
		return super.getOne(queryWrapper);
	}

}
